package com.hice.hcomm.core.collection;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.common.base.CaseFormat;

/**
 * @author <a href="mailto:hecc@ifengmao.com">hecc</a>
 * @version 1.0
 */
public class MapUtils {

    /**
     * 将所有大写字母转小写，并在签名添加下划线；
     *
     * @param param
     * @return
     */
    public static String lowerAndLine(String param) {
        Pattern p = Pattern.compile("[A-Z]");
        if (param == null || param.equals("")) {
            return "";
        }
        StringBuilder builder = new StringBuilder(param);
        Matcher mc = p.matcher(param);
        int i = 0;
        while (mc.find()) {
            builder.replace(mc.start() + i, mc.end() + i, "_" + mc.group().toLowerCase());
            i++;
        }

        if ('_' == builder.charAt(0)) {
            builder.deleteCharAt(0);
        }
        return builder.toString();
    }

    /**
     * 使用谷歌工具类转换小写和下划线
     *
     * @param param
     * @return
     */
    public static String lowerAndLineByGoogle(String param) {
        return CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, param);
    }

    /**
     * 测试
     *
     * @param args
     */
    public static void main(String[] args) {
        System.out.println(lowerAndLine("aaaBbbCCC"));//aaa_bbb_c_c_c
    }

}
